﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;
using BenevolentSun;

namespace Editor
{
    class LevelMenuScreen : Screen
    {

        List<String> menu = new List<String>();

        int selectedMenuEntry = 0;

        SpriteFont font;


        public override void HandleInput()
        {
            if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.DPadDown) ||
                InputManager.IsNewKeyPress(Keys.Down))
            {
                selectedMenuEntry++;
            }
            if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.DPadUp) ||
                InputManager.IsNewKeyPress(Keys.Up))
            {
                selectedMenuEntry--;
            }
            selectedMenuEntry = Math.Max(selectedMenuEntry, 0);
            selectedMenuEntry = Math.Min(selectedMenuEntry, menu.Count - 1);

            if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.A) ||
                InputManager.IsNewKeyPress(Keys.Enter))
            {
                string levelContentName = Editor.GetLevelContentName(menu[selectedMenuEntry]);
                ScreenManager.AddScreen(new LevelEditorScreen(levelContentName));
                ExitScreen();
            }

            base.HandleInput();
        }


        public override void LoadContent()
        {
            font = Content.Load<SpriteFont>("Fonts/Ebrima14");

            foreach (string levelName in Editor.LevelNames)
            {
                menu.Add(levelName);
            }

            base.LoadContent();
        }


        public override void Draw(GameTime gameTime)
        {

            SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);

            SpriteBatch.DrawString(font, "Select A Level: ", new Vector2(20, 20), Color.Yellow);

            for (int i = 0; i < menu.Count; ++i)
            {
                Color color = new Color(51, 51, 51, (byte)(255));
                if (i == selectedMenuEntry)
                    color = new Color(204, 204, 204, (byte)(255));
                SpriteBatch.DrawString(font, menu[i], new Vector2(30, 40 + i * 20), color);
            }

            SpriteBatch.End();

            base.Draw(gameTime);
        }

    }
}
